package com.xzit.rental.security;

import com.alibaba.fastjson.JSON;
import com.xzit.rental.utils.Result;
import com.xzit.rental.utils.ResultCode;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Component
public class LoginFailerHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
                                        HttpServletResponse response,
                                        AuthenticationException exception) throws IOException, ServletException {
        response.setContentType("application/json;charset=utf-8");

        ServletOutputStream outputStream = response.getOutputStream();
        int code = ResultCode.ERROR;
        String message = null;
        if (exception instanceof AccountExpiredException) {
            code=ResultCode.UNAUTHENTICATED;
            message="账户过期";
        } else if (exception instanceof BadCredentialsException) {
            code=ResultCode.UNAUTHENTICATED;
            message="账户或密码错误";
        } else if (exception instanceof CredentialsExpiredException) {
            code=ResultCode.UNAUTHENTICATED;
            message="密码过期";
        }else if (exception instanceof DisabledException) {
            code=ResultCode.UNAUTHORIZED;
            message="账户被禁用";
        } else if (exception instanceof LockedException) {
            code=ResultCode.UNAUTHORIZED;
            message="账户被锁定";
        } else if (exception instanceof InternalAuthenticationServiceException) {
            code=ResultCode.UNAUTHENTICATED;
            message="账户不存在";
        } else if (exception instanceof CustomerAuthenticationException) {
            code=ResultCode.UNAUTHORIZED;
            message=exception.getMessage();
        }else {
            message="登陆失败";
        }
        String result = JSON.toJSONString(Result.fail().setCode(code).setMessage(message));
        outputStream.write(result.getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        outputStream.close();
    }
}
